package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.acticle.domain.*;
import cn.wolfcode.luowowo.acticle.query.ScenicSpotQuery;
import cn.wolfcode.luowowo.acticle.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.acticle.query.TravelQuery;
import cn.wolfcode.luowowo.acticle.service.*;
import cn.wolfcode.luowowo.comment.query.ScenicSpotCommentQuery;
import cn.wolfcode.luowowo.comment.service.IScenicSpotCommentService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * 前端目的地controller
 */
@Controller
@RequestMapping("/destination")
public class DestinationController {

    @Reference
    private IRegionService regionService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private ITravelService travelService;
    @Reference
    private IScenicSpotService scenicSpotService;
    @Reference
    private IScenicSpotThemeService scenicSpotThemeService;

    @Reference
    private IDestinationThemeCatalogService destinationThemeCatalogService;

    @Reference
    private IScenicSpotCommentService scenicSpotCommentService;

    @RequestMapping("")
    public String index(Model model){
        model.addAttribute("hotRegions",regionService.getHotRegions());
        model.addAttribute("dtc",destinationThemeCatalogService.queryAll());
        return "destination/index";
    }

    @RequestMapping("/getHotDestByRegionId")
    public String getHotDestByRegionId(Model model,Long regionId){
        List<Destination> list = destinationService.queryHotDestByRegionId(regionId);

        int size = list.size();
        //左集合 leftDests
        model.addAttribute("leftDests",list.subList(0,size/2 + 1));

        //右集合 rightDests
        model.addAttribute("rightDests",list.subList(size/2 + 1,size));
        model.addAttribute("regionId",regionId);
        return "destination/hotdestTpl";
    }

    @RequestMapping("/guide")
    public String guide(Model model,Long id){
        List<Destination> toasts = destinationService.getToasts(id);
        //集合的最后一个刚好是当前目的地
        Destination dest = toasts.remove(toasts.size() - 1);
        //toasts
        model.addAttribute("toasts",toasts);
        //dest
        model.addAttribute("dest",dest);
        //catalogs
        model.addAttribute("catalogs",strategyCatalogService.getCatalogByDestId(id));
        //strategyDetails
        model.addAttribute("strategyDetails",strategyDetailService.getViewnumTop3(id));

        return "destination/guide";
    }

    @RequestMapping("/surveyPage")
    public String surveyPage(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo){
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination dest = toasts.remove(toasts.size()-1);
        //toasts
        model.addAttribute("toasts",toasts);
        //dest
        model.addAttribute("dest",dest);
        return "destination/survey";
    }

    @RequestMapping("/survey")
    public String survey(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo){
        //catalogs
        List<StrategyCatalog> catalogs = strategyCatalogService.getCatalogByDestId(qo.getDestId());
        model.addAttribute("catalogs",catalogs);
        //catalog
        if(catalogs.size() > 0 && catalogs != null){
            StrategyDetail detail = null;
            for (StrategyCatalog catalog : catalogs) {
                if(catalog.getId() == qo.getCatalogId()){
                    model.addAttribute("catalog",catalog);
                    //detail
                    detail = catalog.getDetails().get(0);
                    //设置攻略明细
                    StrategyContent content = strategyDetailService.getContent(detail.getId());
                    detail.setStrategyContent(content);
                    model.addAttribute("detail",detail);
                    break;
                }
            }
        }

        return "destination/surveyTpl";
    }

    @RequestMapping("/travels")
    public String travels(Model model, @ModelAttribute("qo")TravelQuery qo){
        model.addAttribute("pageInfo",travelService.query(qo));

        return "destination/travelTpl";
    }

    @RequestMapping("/scenicspotlist")
    public String list(Model model, @ModelAttribute("qo") ScenicSpotQuery qo) {
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination dest = null;
        if (toasts.size() > 0) {
            dest = toasts.remove(toasts.size() - 1);
            model.addAttribute("dest", dest);
        }
        //toasts
        model.addAttribute("toasts", toasts);


        //必游景点top5
        List<ScenicSpot> top5= scenicSpotService.queryTop5(qo);
        model.addAttribute("top5",top5);


        //热门前10个
        List<ScenicSpot> hot10= scenicSpotService.queryHot10(qo);
        model.addAttribute("hot10",hot10);

        //景点主题
        List<ScenicSpotTheme> themes= scenicSpotThemeService.listAll(qo.getDestId());
        model.addAttribute("themes",themes);

        //地区父景点内容
        ScenicSpot spot=scenicSpotService.getByDestId(qo.getDestId());
        model.addAttribute("spot",spot);

        return "destination/scenicspotlist";
    }

    @RequestMapping("/scenicspots")
    public String page(Model model, @ModelAttribute("qo") ScenicSpotQuery qo) {
        //默认查询非禁止的
//        qo.setState(1);
        PageInfo pageInfo = scenicSpotService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "destination/scenicTpl";
    }

    @RequestMapping("/scenicspotdetail")
    public String detail(Long detailId, Model model, @ModelAttribute("qo") ScenicSpotCommentQuery qo){
        ScenicSpot detail= scenicSpotService.get(detailId);
        model.addAttribute("detail",detail);

        //查询所有分类的统计
        qo.setRank(-1);
        qo.setScenicId(detailId);
        Long countAll= scenicSpotCommentService.getCount(qo);

        Long rank1= scenicSpotCommentService.getCount1(detailId,1);
        Long rank2= scenicSpotCommentService.getCount1(detailId,2);
        Long rank3= scenicSpotCommentService.getCount1(detailId,3);
        Long rank4= scenicSpotCommentService.getCount1(detailId,4);
        Long rank5= scenicSpotCommentService.getCount1(detailId,5);

        model.addAttribute("rank1",rank1);
        model.addAttribute("rank2",rank2);
        model.addAttribute("rank3",rank3);
        model.addAttribute("rank4",rank4);
        model.addAttribute("rank5",rank5);

        model.addAttribute("countAll",countAll);

        return "destination/scenicspotdetail";
    }
}

